import moment from 'moment';

export default class DateList {
  constructor(beginTime = '', endTime = '') {
    this.currentTime = {
      text: '当天',
      active: true,
      time: moment().format('YYYY-MM-DD HH:mm:ss')
    }
    this.custom = {
      text: '自定义',
      active: false,
      beginTime,
      endTime,
      initDate() {
        this.beginTime = this.endTime = null
      },
      setDate(dateArr) {
        if (dateArr) {
          this.beginTime = moment(dateArr[0]).format('YYYY-MM-DD')
          this.endTime = moment(dateArr[1]).format('YYYY-MM-DD')
        } else {
          this.beginTime = this.endTime = null
        }
      }
    }
    // 默认激活的类型
    this.activeObj = this.currentTime
    // 排序
    this.typeList = [this.currentTime, this.custom]
  }
  // 改变激活对象
  active(itemObj) {
    if (itemObj.active) return
    this.activeObj.active = false
    itemObj.active = true
    this.activeObj = itemObj
    if (itemObj.text == '自定义') itemObj.initDate()
  }

}